package nl.saxion.security.init;

import java.awt.Font;

import javax.swing.JFrame;

import nl.saxion.security.config.Config;
import nl.saxion.security.config.INIConfig;
import nl.saxion.security.controller.ControllerModule;
import nl.saxion.security.controller.FileController;
import nl.saxion.security.gui.GUIModule;
import nl.saxion.security.gui.MainFrame;
import nl.saxion.security.gui.elements.PathPanel;

import nl.saxion.security.serverconfig.ServerConfig;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.name.Names;

public class MainModule extends AbstractModule {

	/**
	 * Configures the Mainmodule Runs the server and iniates several components
	 */
	@Override
	protected void configure() {
		Config config = new INIConfig();
		ServerConfig sConfig = ServerConfig.getSingletonObject(config);
		sConfig.setUpServer();

		bind(Config.class).annotatedWith(Names.named("DefaultConfig"))
				.toInstance(config);

		String fontFamily = config.getAsString(Config.KEY_GUI_FONT_FAMILY,
				"Helvetica");
		int fontSize = config.getAsInt(Config.KEY_GUI_FONT_SIZE, 12);
		Font defaultFont = new Font(fontFamily, Font.PLAIN, fontSize);

		bind(Font.class).annotatedWith(Names.named("DefaultFont")).toInstance(
				defaultFont);

		GUIModule guiModule = new GUIModule();
		guiModule.setConfig(config);
		guiModule.setDefaultFont(defaultFont);

		Injector guiInjector = Guice.createInjector(guiModule);
		MainFrame f = guiInjector.getInstance(MainFrame.class);
		bind(JFrame.class).toInstance(f);

		ControllerModule controllerModule = new ControllerModule(f.getGUI());

		Injector controllerInjector = Guice.createInjector(controllerModule);
		bind(FileController.class).toInstance(
				controllerInjector.getInstance(FileController.class));

		String path = "";
		path = config.getAsString(Config.KEY_SYNC_PATH, path);
		PathPanel.setPathText(path);
	}

}
